﻿using Newtonsoft.Json;
using System;

namespace CMS.Infos
{
    public class DutyRecord
    {
        #region 私有字段
        /// <summary>
        /// 日期.net类型
        /// </summary>
        [JsonIgnore]
        private DateTime? _DateRaw;

        /// <summary>
        /// 开始时间
        /// </summary>
        [JsonIgnore]
        private TimeSpan? _StartTimeRaw;

        /// <summary>
        /// 结束时间
        /// </summary>
        [JsonIgnore]
        private TimeSpan? _EndTimeRaw;

        #endregion

        #region 报文字段
        [JsonProperty(PropertyName = "term_id")]
        public int TermID { get; set; }

        [JsonProperty(PropertyName = "building")]
        public string Building { get; set; }

        [JsonProperty(PropertyName = "assistant_id")]
        public int AssistantID { get; set; }

        [JsonProperty(PropertyName = "date")]
        public string Date { get; set; }

        [JsonProperty(PropertyName = "start_time")]
        public string StartTime { get; set; }

        [JsonProperty(PropertyName = "end_time")]
        public string EndTime { get; set; }

        [JsonProperty(PropertyName = "manual")]
        public bool Manual { get; set; }
        #endregion

        #region 拓展字段
        /// <summary>
        /// 签到日期.net类型
        /// </summary>
        public DateTime DateRaw
        {
            get
            {
                if (_DateRaw == null)
                {
                    _DateRaw = DateTime.Parse(Date);
                }
                return _DateRaw.Value;
            }
            set
            {
                _DateRaw = value;
                Date = value.ToShortDateString();
            }
        }

        /// <summary>
        /// 签到时间
        /// </summary>
        public TimeSpan? StartTimeRaw
        {
            get
            {
                if (StartTime == null)
                {
                    return null;
                }
                else
                {
                    if (_StartTimeRaw == null)
                    {
                        _StartTimeRaw = TimeSpan.Parse(StartTime);
                    }
                    return _StartTimeRaw.Value;
                }
            }
        }

        /// <summary>
        /// 签退时间
        /// </summary>
        public TimeSpan? EndTimeRaw
        {
            get
            {
                if (EndTime == null)
                {
                    return null;
                }
                else
                {
                    if (_EndTimeRaw == null)
                    {
                        _EndTimeRaw = TimeSpan.Parse(EndTime);
                    }
                    return _EndTimeRaw;
                }
            }
        }

        /// <summary>
        /// 值班时长
        /// </summary>
        [JsonIgnore]
        public int? DutyTimeSpan => (int?)(EndTimeRaw - StartTimeRaw)?.TotalMinutes;

        /// <summary>
        /// 星期
        /// </summary>
        public int Week => DateRaw.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)DateRaw.DayOfWeek;

        /// <summary>
        /// 缓存变量
        /// </summary>
        [JsonIgnore]
        public string AssistantName { get; set; }

        /// <summary>
        /// 应值班时间
        /// </summary>
        public uint ShouldDutyClassTime { get; set; }

        /// <summary>
        /// 应签到时间
        /// </summary>
        public string ShouldStartTime { get; set; }

        /// <summary>
        /// 应签退时间
        /// </summary>
        public string ShouldEndTime { get; set; }

        /// <summary>
        /// 备注信息
        /// </summary>
        [JsonIgnore]
        public string Mark { get; set; }
        #endregion

    }
}
